import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    will() {

    }

    @o bannerList = [];
    @o total = 0;
    @o type = '';
    @o currentPage = 1;
    @o firstRequest = false;

    @a
    paginationChange(current) {
        this.currentPage = current;
        this.getBannerList(true);
    }

    addBanner(){
        this.stores.BannerEdit.fire('BannerEdit:show', {
            display:'add',
        });
    }

    @a
    getBannerList(loading){
        loading && this.stores.Frame.showLoading();
        request({
            url:$$.api('getBannerList'),
            method:'get',
            data:{
                pageNumber: this.currentPage,
                pageSize: $$.SEM.BANNER_LIST_SIZE,
                type: this.type || null,
            }
        }).then(res => {
            this.firstRequest = true;
            this.stores.Frame.hideLoading();
            this.bannerList = res.data.list;
            this.total = res.data.total;
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }


    getBannerDelete(id){
        request({
            url:$$.api('getBannerDelete'),
            method:'get',
            data:{
                id:id,
            }
        }).then(res =>{
            this.stores.Message.fire('message', {
                type: $$.SEM.SUCCESS_TEXT,
                text: res.message
            });
            this.getBannerList();
        }).catch(err =>{
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }
    cancelExhibitionRelease(id){
        request({
            url:$$.api('getBannerRelease'),
            method: 'get',
            data:{
                id:id,
            }
        }).then(res => {
            this.stores.Message.fire('message', {
                type: $$.SEM.SUCCESS_TEXT,
                text: res.message
            });
            this.getBannerList();
        }).catch(err =>{
            this.stores.Message.fire('message', {
                type: $$.SEM.ERROR_TEXT,
                text: err.message
            });
        })
    }

    typeChange(v){
        this.type = v.value || '';
        this.currentPage = 1;
        this.getBannerList()
    }

    editClick(data){
        this.stores.BannerEdit.fire('BannerEdit:show', {
            display:'edit',
            detail: data,
        });
    }

    releaseClick(data){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            title: data.isShare === 1 ? '取消发布' : '发布',
            tips: data.isShare === 1 ?
                (data.type === 1 || data.type === 2 ?'取消发布后，画展信息将不会显示在banner位':'取消发布后，用户信息将不会显示在banner位')
                :'确认发布吗？',
            func: () => {
                this.cancelExhibitionRelease(data.id)
            }
        });
    }

    deleteClick(data){
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips:$$.LN.ARE_YOU_SURE_YOU_WANT_TO_DELETE,
            func: () => {
                this.getBannerDelete(data.id)
            }
        });
    }


    mount() {
        this.getBannerList();
    }

}

export default Store;